package cn.hh.pet.controller;


import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.PageList;
import cn.hh.pet.domain.Pet;
import cn.hh.pet.domain.PetDetail;
import cn.hh.pet.query.PetQuery;
import cn.hh.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;
    /*编辑*/
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        /*通过id判断是新增还是删除*/
        try {
            System.out.println("接收的数据"+pet);
            petService.addOrUpdate(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*删除*/
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            petService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*批量删除*/
    @PatchMapping
    public AjaxResult deleteByIds(@RequestBody Long[] ids){
        try {
            petService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*批量删除*/
    @PatchMapping("/onsale")
    public AjaxResult onsale(@RequestBody Long[] ids){
        try {
            petService.onsaleByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*批量删除*/
    @PatchMapping("/offsale")
    public AjaxResult offsale(@RequestBody Long[] ids){
        try {
            for (int i = 0; i < ids.length; i++) {
                System.out.println(ids[i]);
            }
            petService.offsaleByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*查询所有*/
    @GetMapping
    public List<Pet> loadAll(){
        return petService.loadAll();
    }
    /*查询一个*/
    @GetMapping("/{id}")
    public Pet loadById(@PathVariable("id")Long id){
        return petService.loadById(id);
    }
    /*查询图片数据*/
    @GetMapping("/detail/{id}")
    public PetDetail loadDetailById(@PathVariable("id") Long id){
        PetDetail petDetail = petService.loadDetailById(id);
        return petDetail;
    }
    /*分页*/
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        return petService.queryPage(query);
    }
    /*上架商品查询*/
    @PostMapping("/list")
    public PageList<Pet> listqueryPage(@RequestBody PetQuery query){
        query.setState(1);
        return petService.listqueryPage(query);
    }
}
